//js 调用
$(function(){
   //弹窗
   $('#myModal').modal();
   //构造web3对象
   var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
   web3.eth.getAccounts(function(e,r){
      console.log(e,r);
   });
   //构造crowd合约对象
   var crowdContractAddr = "0xf4f57911397017e53a0f8b7c9f61cb8bd1f4bbd7";
   var ownerAddr = "0xd8b44bfcb0ece666511eeefd4d5e129a3ca5bcca";
   var crowdObj = new web3.eth.Contract(crowdAbi, crowdContractAddr);
   var acctAddr;
   var kccContractAddr;
   var mvcContractAddr;
   var kccCounts = 0;
   var mvcCounts = 0;
   var kccObj;
   var mvcObj;
   //构造mvc对象

   console.log(crowdObj);
   function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y+M+D+h+m+s;
    }


   //登陆按钮被点击
   $(".Login").on("click",function(){
      acctAddr = $("#addressId").val();
      console.log("get acct:",acctAddr);
      $(".close_win").click();
      crowdObj.methods.getAddr().call(function(e,r){
        if(!e) {
            kccContractAddr = r.kaddr;
            mvcContractAddr = r.maddr;
            console.log("get addr:",kccContractAddr,mvcContractAddr);
            kccObj = new web3.eth.Contract(kccAbi, kccContractAddr);
            mvcObj = new web3.eth.Contract(mvcAbi, mvcContractAddr);
            alert("连接成功");
            }
            else {
            alert("连接失败");
            }
       });
   });
   //充值kcc
   $(".Recharge").on("click",function(){

     kccCounts = $("#kccCounts").val();

    kccObj.methods.airDrop(acctAddr, kccCounts*1000).send(
      {
          from : ownerAddr,//owner
          gas : 300000
      },function(e,r){
                if(!e) {
                   alert("充值成功");
                }
                else {
                   alert("失败");
                   console.log("充值失败：",e);
                }
             });
   });

   //投票处理- 此时可以认为合约地址都已经拿到,kcc合约对象，mvc合约地址
   $(".Vote").on("click",function(){
      mvcCounts = $("#mvcCounts").val();

      //2. 调用kcc转账给owner,至少1000 kcc
      kccObj.methods.transfer(ownerAddr, mvcCounts*1000).send(
         {
            from : acctAddr,
            gas  : 300000
         },function(e,r){
            if(!e) {
               console.log("transfer ok:");
               //3. 调用mvc的空投给acctAddr
               mvcObj.methods.airDrop(acctAddr,mvcCounts*100).send(
                  {
                     from : ownerAddr,
                     gas  : 300000
                  },function(e,r){
                     if(!e) {
                        alert("投票成功");
                     }
                     else {
                        alert("投票失败");
                        console.log("失败:",e);
                     }
               });
            }
      });
   });
   $(".Refresh").on("click",function(){
     var kccamount;
     var mvcamount;
     var cdTime;
     var mvcTotal;
     var pcs;
     kccObj.methods.balanceOf(acctAddr).call(function(e,r){
       kccamount = r;
       document.getElementById("kccs").innerText = kccamount;

     });
     mvcObj.methods.crowInfo(acctAddr).call(function(e,r){
       mvcamount = r.amount;
       cdTime = timestampToTime(r.crowdTime);
       document.getElementById("mvcs").innerText = mvcamount;

       document.getElementById("dates").innerText = cdTime;

     });
     mvcObj.methods.getTotal().call(function(e,r){
       mvcTotal = r;
       pcs = mvcamount*100/mvcTotal;
       document.getElementById("pcs").innerText = pcs+"%";
    });
   });
});
